package com.his.menu.service.Imp;

import com.his.dictrole.dao.DictRoleEmpMapper;
import com.his.dictrole.entity.DictRoleEmp;
import com.his.dictrole.service.DictRoleService;
import com.his.login.entity.DictEmployee;
import com.his.menu.dao.BenchWorkIndexMapper;
import com.his.menu.entity.BenchWorkIndex;
import com.his.menu.service.MenuService;
import com.his.menu.vo.MenuVo;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.PinYin4jUtil;
import com.xiao.common.util.WorkIdUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author xiaoxiaoping
 * @Date 2020/1/21
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private BenchWorkIndexMapper benchWorkIndexMapper;
    @Resource
    private PinYin4jUtil pinYin4jUtil;
    @Resource
    private DictRoleService dictRoleService;

    /**
     * 插入菜单
     * @param menuVo
     * @throws Exception
     */
    @Override
    public void insert(MenuVo menuVo) throws Exception {
        BenchWorkIndex benchWorkIndex1 = new BenchWorkIndex();
        benchWorkIndex1.setINDEXID(menuVo.getINDEXID());
        List<BenchWorkIndex> list = benchWorkIndexMapper.selectByEntity(benchWorkIndex1);
        if(list.size()>0){
            throw new Exception("菜单编码已存在！");
        }
        BenchWorkIndex benchWorkIndex = BeanUtil.copyBean(menuVo,BenchWorkIndex.class);
        benchWorkIndex.setBENCHID(WorkIdUtil.nextId());
        benchWorkIndex.setINVALID(1);
        benchWorkIndex.setSYSFLAG(1);
        benchWorkIndex.setICONO("");
        benchWorkIndexMapper.insertAllColumn(benchWorkIndex);
    }

    @Override
    public List<Map<String,Object>> getMenuList() {
        List<BenchWorkIndex> list = benchWorkIndexMapper.selectAll();
        List<Map<String, Object>> menuList = new ArrayList<>();
        if(list.size()>0){
            //一级菜单
            for(BenchWorkIndex benchWorkIndex : list){
                menuList.add(toNode(benchWorkIndex));
            }
            //list转成tree
            return getTree(menuList,null,"id");
        }

        return null;
    }

    @Override
    public List<BenchWorkIndex> getAllMenu(MenuVo menuVo) {
        BenchWorkIndex benchWorkIndex = BeanUtil.copyBean(menuVo,BenchWorkIndex.class);
        return benchWorkIndexMapper.selectByEntity(benchWorkIndex);
    }

    private Map<String ,Object> toNode(BenchWorkIndex benchWorkIndex){
//        {
//            id: 1, // 唯一id
//                    key: '', // 唯一key
//                name: '首页', // 菜单汉字名称
//                pinyin: 'shouye', // 菜单拼音名称
//                path: '/', // 菜单路由
//                icon: 'iconfont icon-index', // 菜单icon类名
//                pid: '', // 父id
//                isEdit: false, // 是否在编辑该节点
//                children: [] // 菜单子节点
//        }
        Map<String, Object> node = new HashMap<>();
        node.put("id",benchWorkIndex.getBENCHID());
        node.put("key",benchWorkIndex.getINDEXID());
        node.put("name",benchWorkIndex.getINDEXNAME());
        node.put("pinyin",benchWorkIndex.getFULLCODE());
        node.put("path",benchWorkIndex.getINDEXPATH());
        node.put("icon",benchWorkIndex.getICONPATH());
        node.put("pid", benchWorkIndex.getUPID());
        node.put("grade",benchWorkIndex.getGRADE());
        node.put("isEdit",false);
        return node;
    }

    /**
     *
     * @param list 所有元素的平级集合，map包含id和pid
     * @param pid 顶级节点的pid，可以为null
     * @param idName id位的名称，一般为id或者code
     * @return 树
     */
    public static List<Map<String, Object>> getTree(List<Map<String, Object>> list, String pid, String idName) {
        List<Map<String, Object>> res = new ArrayList<Map<String,Object>>();
        if (CollectionUtils.isNotEmpty(list))
            for (Map<String, Object> map : list) {
                if(pid == null && map.get("p"+idName) == null || map.get("p"+idName) != null && map.get("p"+idName).equals(pid)){
                    String id = (String) map.get(idName);
                    map.put("children", getTree(list, id, idName));
                    res.add(map);
                }
            }
        return res;
    }

    /**
     * 用户权限菜单
     * @param dictEmployee
     * @return
     */
    @Override
    public List<Map<String, Object>> getMenuListByEmp(DictEmployee dictEmployee) {
        List<BenchWorkIndex> list = benchWorkIndexMapper.selectMenuByEmp(dictEmployee.getEMPID());
        HashSet<BenchWorkIndex> hashSet = new HashSet<BenchWorkIndex>(list);
        Set<BenchWorkIndex> set = hashSet;
        List<Map<String, Object>> menuList = new ArrayList<>();
        List<Map<String, Object>> toTreeList = new ArrayList<>();
        if(set.size()>0){
            //一级菜单
            for(BenchWorkIndex benchWorkIndex : set){
                menuList.add(toNode(benchWorkIndex));
            }
            //list转成tree
            return getTree(menuList,null,"id");
        }

        return null;
    }

    /**
     * 修改菜单
     * @param menuVo
     */
    @Override
    public void updateById(MenuVo menuVo) {
        BenchWorkIndex benchWorkIndex = BeanUtil.copyBean(menuVo,BenchWorkIndex.class);
        benchWorkIndexMapper.updateById(benchWorkIndex);
    }

    /**
     * 根据benchids 查菜单
     * @param roleId
     * @return
     */
    @Override
    public List<Map<String, Object>> getMenuListByRoleId(String roleId) {
       List<String> menuList = dictRoleService.selectMenuByRole(roleId);
        List<BenchWorkIndex> benchWorkIndexList = benchWorkIndexMapper.selectByIdSet(menuList);
        List<Map<String, Object>> menuList1 = new ArrayList<>();
        if(benchWorkIndexList.size()>0){
            //一级菜单
            for(BenchWorkIndex benchWorkIndex : benchWorkIndexList){
                menuList1.add(toNode(benchWorkIndex));
            }
            return menuList1;
        }

        return null;
    }

    /**
     * 获取菜单的id数组
     * @param empId
     * @return
     */
    @Override
    public List<String> getMenuByEmp(String empId) {
        List<BenchWorkIndex> benchWorkIndexList = benchWorkIndexMapper.selectMenuByEmp(empId);
        List<String> menuList = new ArrayList<>();
        for(BenchWorkIndex benchWorkIndex : benchWorkIndexList){
            menuList.add(benchWorkIndex.getBENCHID());
        }
        return menuList;
    }
}
